package com.yourname.bank_sim.list;

import com.yourname.bank_sim.entity.Account;
import com.yourname.bank_sim.entity.ActionLog;

import java.util.ArrayList;

public class CustomList<E> extends ArrayList<E> {
    public int indexOfByAccount(String account){
        for (int i=0;i<this.size();i++){
            E e = this.get(i);
            if (e instanceof Account){
                if (((Account) e).getAccountNumber().equals(account)){
                    return i;
                }
            }else if (e instanceof ActionLog){
                if (((ActionLog) e).getActor().equals(account)){
                    return i;
                }
            }else{
                System.out.println("类型不正确");
                return -1;
            }
        }
        return -1;
    }
    public E getObjectByAccountNumber(String account, Object type){
        if (type instanceof Account){
            for (E e:this){
                Account a=(Account) e;
                if (a.getAccountNumber().equals(account)){
                    return (E) a;
                }
            }
            System.out.println("没有账号为"+account+"的账户。");
        }else if(type instanceof ActionLog){
            for (E e:this){
                ActionLog a=(ActionLog) e;
                if (a.getActor().equals(account)){
                    return (E) a;
                }
            }
            System.out.println("账号为"+account+"的账户没有任何操作记录。");
        }else{
            System.out.println("类型不正确");
            return null;
        }
        return null;
    }
    public boolean updateByAccount(String account,E newObject){
        if (newObject instanceof Account){
            int i = indexOfByAccount(account);
            if (i!=-1){
                this.set(i,newObject);
                return true;
            }else{
                System.out.println("没有账号为"+account+"的账户。");
                return false;
            }
        }else if(newObject instanceof ActionLog){
            int i = indexOfByAccount(account);
            if (i!=-1){
                this.set(i,newObject);
                return true;
            }else{
                System.out.println("账号为"+account+"的账户没有任何操作记录。");
                return false;
            }
        }else {
            System.out.println("类型不正确");
            return false;
        }
    }
    public boolean deleteByAccount(String account,E newObject){
        if (newObject instanceof Account){
            int i = indexOfByAccount(account);
            if (i!=-1){
                this.remove(i);
                return true;
            }else{
                System.out.println("没有账号为"+account+"的账户。");
                return false;
            }
        }else if(newObject instanceof ActionLog){
            int i = indexOfByAccount(account);
            if (i!=-1){
                this.remove(i);
                return true;
            }else{
                System.out.println("账号为"+account+"的账户没有任何操作记录。");
                return false;
            }
        }else {
            System.out.println("类型不正确");
            return false;
        }
    }

}
